package com.deyuanyun.pic.pipe.controller;

import com.caixu.beanvalidateutil.imlclass.BeanValidateUtil;
import com.caixu.beanvalidateutil.imlclass.VResult;
import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.tool.StaticParam;
import com.deyuanyun.pic.common.util.MyCollectionUtil;
import com.deyuanyun.pic.common.util.RequestUtil;
import com.deyuanyun.pic.common.util.ajax.AjaxResponse;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.poi.ReadExcel;
import com.deyuanyun.pic.pipe.domain.exceldomain.ValveRoomImportDemo;
import com.deyuanyun.pic.pipe.service.ValveRoomService;
import com.deyuanyun.pic.pipe.support.utils.ValveRoomCenterPoint;
import com.deyuanyun.pic.support.utils.ExcelUtils;
import com.github.pagehelper.Page;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 导入场站阀室基本信息
 * 
 * @author scalpel
 * @version 1.0
 * @date 2016年1月26日上午10:01:41
 * Method List:                       // 主要函数及其功能
 *             1.---------------- 
 *             search 搜索场站阀室数据
 *             import 导入场站阀室信息
 *             del    删除场站阀室数据
 *             gisdel 删除场站阀室中心线数据
 *             exportExl 导出
 *             
 *             
 *History:                           // 历史修改记录
 */
@Controller
@RequestMapping("/pipe/valueRoom")
public class PipeValueRoomController extends PipeBaseController{

	@Resource
	private ValveRoomService pipeValveRoomService;
	
//	@RequestMapping("/page.html")
//	public ModelAndView page(HttpServletRequest request){
//		ModelAndView mdv=new ModelAndView();
//		
//		
//		return mdv;
//	}
//	
	@RequestMapping("/search.json")
	@ResponseBody
	public void search(HttpServletRequest request){
		
		HashMap map=RequestUtil.parseRequestParamToHashMap(request);
		PageJsonBean jsonBean=new PageJsonBean();
		Page main=pipeValveRoomService.findPage(map);
		
		jsonBean.setList(main);
		AjaxSupport.sendSuccessText("ok", jsonBean);
		
		try {
			pipeValveRoomService.updateCenterPoint();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping("/import.json")
	@ResponseBody
	public void importData(MultipartFile file,HttpServletResponse response) throws IOException, Exception{
		List<ValveRoomImportDemo> data= ReadExcel.readExcle(ValveRoomImportDemo.class,file.getInputStream(),4,"9");
		data= MyCollectionUtil.removeLastEmpty(data);
		VResult b= BeanValidateUtil.vali2(data,4+"");
		if (b.isRelult()){
			String result= pipeValveRoomService.importData(data);
			if(result == null)
			{
				 AjaxResponse.sendText(response, StaticParam.UPLOAD_FILE_SUCCESS);
			}else{
				 response.setStatus(503);
		         AjaxResponse.sendText(response,StaticParam.getUploadFileFailMessage(result));
			}
			
		}else{
			String[] titleArr= ReadExcel.getTitle(file,"in/valve_room_t.xls");
            String m=b.getAllString(titleArr);
            response.setStatus(503);
            AjaxResponse.sendText(response,StaticParam.getUploadFileFailMessage(m));
		}
	}
	
	
	@RequestMapping("/del.json")
	@ResponseBody
	public void del(HttpServletRequest request){
		HashMap map=RequestUtil.parseRequestParamToHashMap(request);
		pipeValveRoomService.delInfo(map);
		Map<String,String> result=new HashMap<String,String>();
		result.put("status", "0");
		AjaxSupport.sendSuccessText("ok", result);
	}
	
	@RequestMapping("/gisdel.json")
	@ResponseBody
	public void gisdel(HttpServletRequest request){
		HashMap map=RequestUtil.parseRequestParamToHashMap(request);
		pipeValveRoomService.delGis(map);
		Map<String,String> result=new HashMap<String,String>();
		result.put("status", "0");
		AjaxSupport.sendSuccessText("ok", result);
		
		
		
	}
	
	@RequestMapping("/exportExl.json")
	@ResponseBody
	public void exportExl(HttpServletRequest request,HttpServletResponse response){
		
			HashMap param=RequestUtil.parseRequestParamToHashMap(request);
			InputStream filePath =this.getClass().getResourceAsStream("/template/excl/basedata/controlPoint_template.xls");
			int[] lineStart = new int[] {2 };
			List<List<String[]>> result=pipeValveRoomService.excelExport(param);
			ExcelUtils.jxlExportExcel(filePath, lineStart, "场站阀室", result, response);
	}

	
	@RequestMapping("/findGis.json")
	@ResponseBody
	public void findGis(HttpServletRequest request,String id) throws Exception{
		Map<String, Object> data=pipeValveRoomService.findValveRoomCenterPointById(id);
		
		StringBuffer json=new StringBuffer("{");
		json.append("\"lng\":").append(data.get("CONVERT_X")).append(",").append("\"lat\":").append(data.get("CONVERT_Y")).append(",");
		json.append("\"name\":\"").append(data.get("SHOW_NAME")).append("\",").append("\"state\":").append(data.get("ISCGSZ"));
		json.append("}");
		Map<String,String> m=new HashMap<String,String>();
		m.put("data", json.toString());
		m.put("type", "1");
		AjaxSupport.sendSuccessText("ok", m);
	}
	
	
	/**
	 * 
	  * 获取所有场站阀室中心点信息
	  *
	  * @param 
	  * @return 
	  * @throws  
	  * @see    
	  * Table Accessed: 
	  * Table Updated: 
	  * Other:       
	  *
	 */
	@RequestMapping("/findValveRoomCenterPoint.json")
	@ResponseBody
	public void findValveRoomCenterPoint(HttpServletRequest request,String id){
		List<HashMap<String, Object>> data=pipeValveRoomService.findValveRoomCenterPoint();
		StringBuffer json=new StringBuffer("[");
		for(HashMap<String,Object> v : data){
			json.append("{");
			json.append("\"lng\":").append(v.get("CONVERT_X")).append(",").append("\"lat\":").append(v.get("CONVERT_Y")).append(",");
			json.append("\"name\":\"").append(v.get("SHOW_NAME")).append("\",").append("\"state\":").append(v.get("ISCGSZ"));
			json.append("},");
		}
		if(data.size() >0)
		{
			json.deleteCharAt(json.length()-1);
		}
		
		json.append("]");
		Map<String,String> m=new HashMap<String,String>();
		m.put("data", json.toString());
		m.put("type", "1");
		AjaxSupport.sendSuccessText("ok", m);
	}
}
